package cn.czh.org.service.impl;

import cn.czh.basic.exception.BusinessException;
import cn.czh.basic.service.impl.BaseServiceImpl;
import cn.czh.basic.util.BaiduAiUtils;
import cn.czh.basic.util.Md5Utils;
import cn.czh.basic.util.StrUtils;
import cn.czh.org.domain.Employee;
import cn.czh.org.domain.Shop;
import cn.czh.org.domain.ShopAuditLog;
import cn.czh.org.domain.ShopDto;
import cn.czh.org.mapper.EmployeeMapper;
import cn.czh.org.mapper.ShopAuditLogMapper;
import cn.czh.org.mapper.ShopMapper;
import cn.czh.org.service.IShopService;
import cn.czh.user.domain.Logininfo;
import cn.czh.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.tools.JavaFileManager;
import java.util.List;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        String address = shop.getAddress();
        String name = shop.getName();
        String tel = shop.getTel();
        //校验
        //1.校验空值
        if (StringUtils.isEmpty(address) || StringUtils.isEmpty(name) || StringUtils.isEmpty(tel)){
            throw new BusinessException("数据不能为空");
        }
        //2.校验密码是否一致[在domain/Employee中加一个comfirmPassword字段]
        //3.校验店铺是否已经入驻过
        Shop dbShop = shopMapper.loadByNameAndAddress(shop);
        if (dbShop !=null){ //入驻过
            throw new BusinessException("该店铺已经入驻,请直接登录");
        }

        //4.内容审核
        //文本审核
        if(!BaiduAiUtils.textCensor(shop.getName())){
            throw new BusinessException("店铺名称非法");
        }
        //图片审核
        String imgUrl = "http://123.207.27.208"+shop.getLogo();
        if (!BaiduAiUtils.imgCensor(imgUrl)){
            throw new BusinessException("店铺Logo非法");
        }

        //保存数据
        //1.保存店铺管理员信息
        Employee admin = shop.getAdmin();
        //设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        admin.setSalt(salt);
        //设置密码
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setPassword(md5Pwd);


        //保存logininfo的信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin, logininfo);
        logininfo.setType(0); //设置管理员
        logininfoMapper.save(logininfo);
        //将Logininfo_id设置到employee中
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.save(admin); //这一步执行完就有employee_id

        //2.保存店铺信息
        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);

        //3.将shop的Id设置到employee中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
        //1.改店铺状态 1 - 2
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(2);
        shopAuditLog.setAuditId(325L);
        shopAuditLogMapper.save(shopAuditLog);

        //3.发送激活邮件 @TODO
        //发送复杂邮件
        //创建复杂邮件对象
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        //设置发件人
        helper.setFrom("1422675248@qq.com");
        //设置收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        helper.setTo(employee.getEmail());
        //邮件主题
        helper.setSubject("店铺审核通过");
        //邮件内容
        helper.setText("<h1>店铺激活邮件<br/><a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击激活</a></h1>",true);
        //发送邮件
        javaMailSender.send(message);


    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        //1.改店铺状态 1 - 4
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(4);
        shopAuditLog.setAuditId(325L);
        shopAuditLogMapper.save(shopAuditLog);

        //3.发送激活邮件 @TODO
        //发送复杂邮件
        //创建复杂邮件对象
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        //设置发件人
        helper.setFrom("1422675248@qq.com");
        //设置收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        helper.setTo(employee.getEmail());
        //邮件主题
        helper.setSubject("店铺审核失败");
        //邮件内容
        helper.setText("<h1>店铺重新入驻<br/>" +
                "<a href='http://localhost:8081/#/register'>点击重新入驻</a></h1>",true);
        //发送邮件
        javaMailSender.send(message);

        //4.删除入驻信息
        employeeMapper.remove(employee.getId());
        shopMapper.remove(shop.getId());
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}
